package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.domain.LoginForm;
import com.example.hospital.api.common.R;
import com.example.hospital.api.service.MisUserService;
import com.tencentcloudapi.tcm.v20210413.models.IngressGateway;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mis_user")
public class MisUserController {
@Resource
    private MisUserService misUserService;

@PostMapping("/login")

    public R login(@RequestBody @Valid LoginForm form){
        Map param = BeanUtil.beanToMap(form);
        Integer userId = misUserService.login(param);
    //登录成功
        if (userId != null){
            StpUtil.login(userId);
            //获取token
            String token =StpUtil.getTokenValue();
            List<String> permissions = StpUtil.getPermissionList();
            return R.ok().put("result",true).put("token",token).put("permissions",permissions);

        }

    return R.ok().put("result",false);
    }

//退出登录
    @GetMapping("/logout")
    //验证用户是否登录过,没有登录过的话不能继续
    @SaCheckLogin
    public R logout() {
        //调用工具类
        StpUtil.logout();
        return R.ok();
    }

}
